﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CXedu.Model;
using CXedu.Business;
using CXedu.WebSite.Code;
using System.Collections;
using Ajeelee.Core.Utils;

namespace CXedu.WebSite
{
    public partial class detail : System.Web.UI.Page
    {
        protected int id;
        Hashtable ht = new Hashtable();
        protected News newsInfo = new News();
        protected NewsClass newsClass = new NewsClass();
        protected IList<News> ortherNewsInfo = new List<News>();

        INewsBusiness newsBll = new NewsBusinessFactory().CreateNewsBusiness();

        protected void Page_Load(object sender, EventArgs e)
        {
            // 获取参数
            id = Convert.ToInt32(Request.QueryString["id"]);

            if (id > 0)
            {
                try
                {
                    // 读取新闻信息
                    newsInfo = newsBll.GetById(id);
                    if (newsInfo != null)
                    {
                        // 读取分类信息
                        newsClass = Utility.GetNewsClass().FirstOrDefault(c => c.Id == newsInfo.CId);
                        if (newsClass == null)
                        {
                            newsClass = new NewsClass();
                        }

                        // 处理导航
                        NavigationType currNav = NavigationType.Index;
                        if (Enum.IsDefined(typeof(NavigationType), "List_" + newsClass.Id))
                        {
                            currNav = (NavigationType)Enum.Parse(typeof(NavigationType), "List_" + newsClass.Id);
                        }
                        else if (newsClass.PId == 15)
                        {
                            currNav = NavigationType.Train;
                        }
                        else if ("2,3,4,5".Contains(newsInfo.CId.ToString()))
                        {
                            currNav = NavigationType.AdultEdu;
                        }
                        this.header1.CurrentNavType = this.right1.CurrentNavType = currNav;

                        //相关新闻 ortherNewsInfo
                        if (newsInfo.Keyword.IsNotNullAndEmpty())
                        {
                            string[] keys = newsInfo.Keyword.Split(new char[] { ',', '-' });
                            string where = "";
                            if (keys.Length > 0)
                            {
                                for (int i = 0; i < keys.Length; i++)
                                {
                                    where += (where.IsNotNullAndEmpty() ? "or " : "") + "N_Title like '%" + keys[i] + "%' or N_Keyword like '%" + keys[i] + "%' ";
                                }
                                if (where.IsNotNullAndEmpty())
                                {
                                    where = string.Format("({0} and N_Id <> {1})", where, newsInfo.Id);
                                    ht.Clear();
                                    ht.Add(NewsHash.TopNum, 5);
                                    ht.Add(NewsHash.CustomWhere, where);
                                    ortherNewsInfo = newsBll.GetAllNews(ht);
                                }
                            }
                        }
                        // 点击统计
                        ht.Clear();
                        ht.Add(NewsHash.Id, id);
                        ht.Add(NewsHash.Count, newsInfo.Count + 1);
                        newsBll.UpdateSomeVal(ht);
                    }
                }
                catch (System.Threading.ThreadAbortException ex) { }
                catch (Exception ex)
                {
                    Function.WriteErrorLog(ex);
                }
            }
        }
    }
}